package com.tensquare.article.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2021/1/3 21:54
 */
@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController
{
    @Autowired
    private ArticleService articleService;

    // GET /article 文章全部列表
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll()
    {
        List<Article> list = articleService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    // GET /article/{articleId} 根据ID查询文章
    @RequestMapping(value = "{articleId}", method = RequestMethod.GET)
    public Result findById(@PathVariable String articleId)
    {
        Article article = articleService.findById(articleId);
        return new Result(true, StatusCode.OK, "查询成功", article);
    }

    // POST /article 增加文章
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Article article)
    {
        articleService.save(article);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    // PUT /article/{articleId} 修改文章
    @RequestMapping(value = "{articleId}", method = RequestMethod.PUT)
    public Result updateById(@PathVariable String articleId,
                             @RequestBody Article article)
    {
        //设置id
        article.setId(articleId);
        // 执行修改
        articleService.updateById(article);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    // DELETE /article/{articleId} 根据ID删除文章
    @RequestMapping(value = "{articleId}", method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String articleId)
    {
        articleService.deleteById(articleId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    // POST /article/search/{page}/{size} 文章分页
    @RequestMapping(value = "search/{page}/{size}", method = RequestMethod.POST)
    public Result findByPage(@PathVariable("page") Integer page,
                             @PathVariable("size") Integer size,
                             @RequestBody Map<String, Object> map)
    {
        //根据条件分页查询
        Page<Article> pageData = articleService.findByPage(map, page, size);
        //封装分页返回对象
        PageResult<Article> pageResult = new PageResult<>(
                pageData.getTotal(), pageData.getRecords()
        );
        //返回数据
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    //测试公共异常处理
    @RequestMapping(value = "exception", method = RequestMethod.GET)
    public Result test()
    {
        int a = 1 / 0;
        return null;
    }

    //根据文章id和用户id，建立订阅关系。保存的是文章作者id和用户id的关系
    //http://127.0.0.1:9004/article/subscribe POST
    @RequestMapping(value = "subscribe", method = RequestMethod.POST)
    public Result subscribe(@RequestBody Map map)
    {
        //返回状态，如果返回true就是订阅该文章作者，如果返回false就是取消订阅文章作者
        Boolean flag = articleService.subscribe(map.get("articleId").toString(),map.get("userId").toString());
        //判断订阅还是取消订阅
        if (flag)
        {
            return new Result(true, StatusCode.OK, "订阅成功");
        }
        return new Result(true, StatusCode.OK, "取消订阅成功");
    }
}
